package org.sjs.dreamfly.data

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import org.sjs.dreamfly.common.util.PhemonUtils
import org.sjs.dreamfly.model.HistoryItem
import org.sjs.dreamfly.model.MovieInfo
import org.sjs.dreamfly.model.VideoInfo

@Database(entities = [MovieInfo::class, VideoInfo::class, HistoryItem::class], version = 1)
abstract class AppDataBase: RoomDatabase() {
    abstract fun movieDao(): MovieInfoDao
    abstract fun videoDao(): VideoInfoDao
    abstract fun historyDao(): HistoryItemDao

    companion object {
        @Volatile
        private var INSTANCE: AppDataBase? = null
        fun getDatabase(context: Context = PhemonUtils.getApplication()): AppDataBase {
            if (INSTANCE == null) {
                synchronized(this) {
                    INSTANCE = Room.databaseBuilder(
                        context.applicationContext,
                        AppDataBase::class.java,
                        "phemon.db"
                    ).fallbackToDestructiveMigration().build()
                    return INSTANCE!!
                }
            } else {
                return INSTANCE!!
            }
        }
    }
}